export default {
	data: () => {
		return {
			// 通用静态数据
			dictSexOptions: [{
				text: '男',
				value: 1
			}, {
				text: '女',
				value: 2
			}],
			dictStatusOptions: [{
				text: '启用',
				value: 1
			}, {
				text: '禁用',
				value: 0
			}],
			// 登录页校验拦截配置
			configLogin: {
				whiteList: ['/pages/login/login', '/pages/index/index'],
				loginPage: '/pages/login/login'
			}
		}
	},
	onShareAppMessage() {
		const pages = getCurrentPages();
		const currentPage = pages[pages.length - 1];
		const pagePath = currentPage.route;

		return {
			title: '分享给微信好友',
			path: pagePath
		};
	},
	onShareTimeline() {
		const pages = getCurrentPages();
		const currentPage = pages[pages.length - 1];
		const pagePath = currentPage.route;

		return {
			title: '分享到朋友圈',
			path: pagePath
		};
	},
	methods: {
		// ==================== 文件上传相关方法 ====================

		// UNI-UI上传文件通用回调
		fileProgress(val) {
			console.log("文件上传进度:", val)
		},
		fileSuccess(val) {
			console.log("文件上传成功:", val)
		},
		fileFail(val) {
			console.log("文件上传失败:", val)
		},
		fileSelect(val) {
			console.log("文件选择:", val)
		},
		// 通用文件上传方法
		uploadFileApi(options = {}) {
			const {
				maxSize = 10, // 默认最大10MB
				uploadUrl = '/upload', // 默认上传地址
				onSuccess = () => {},
				onFail = () => {},
				fileType = 'image' // 文件类型：image, file
			} = options;

			// 选择文件
			const chooseFile = () => {
				// #ifdef MP-WEIXIN
				if (fileType === 'image') {
					uni.chooseMedia({
						count: 1,
						mediaType: ['image'],
						success: (res) => {
							this.handleFileUpload(res.tempFiles[0], uploadUrl, maxSize, onSuccess, onFail);
						},
						fail: onFail
					});
				} else {
					wx.chooseMessageFile({
						count: 1,
						type: 'file',
						success: (res) => {
							this.handleFileUpload(res.tempFiles[0], uploadUrl, maxSize, onSuccess, onFail);
						},
						fail: onFail
					});
				}
				// #endif

				// #ifndef MP-WEIXIN
				uni.chooseFile({
					count: 1,
					success: (res) => {
						this.handleFileUpload(res.tempFiles[0], uploadUrl, maxSize, onSuccess, onFail);
					},
					fail: onFail
				});
				// #endif
			};

			chooseFile();
		},

		// 处理文件上传
		handleFileUpload(file, uploadUrl, maxSize, onSuccess, onFail) {
			// 检查文件大小
			if (file.size > maxSize * 1024 * 1024) {
				uni.showToast({
					title: `文件大小不可超过${maxSize}MB`,
					icon: 'none'
				});
				return;
			}

			uni.showLoading({
				title: '上传中...',
				mask: true
			});

			// 使用封装的http上传方法
			this.$http.upload(uploadUrl, file.path || file.tempFilePath, {}, {
				name: 'file'
			}).then(res => {
				onSuccess({
					filePath: res.url || res.data,
					fileName: file.name || '文件'
				});
			}).catch(err => {
				onFail(err);
			}).finally(() => {
				uni.hideLoading();
			});
		},
		// ==================== 页面导航相关方法 ====================

		// 页面跳转
		goPage(path, type = 'navigateTo') {
			if (!path) {
				uni.showToast({
					title: '暂无跳转地址',
					icon: 'none'
				});
				return;
			}

			uni[type]({
				url: path,
				fail: (err) => {
					console.error('页面跳转失败:', err);
					uni.showToast({
						title: '页面跳转失败',
						icon: 'none'
					});
				}
			});
		},

		// ==================== 工具方法 ====================

		// 复制到剪贴板
		handlerCopy(value, type = 'set') {
			if (type === 'set') {
				uni.setClipboardData({
					data: value,
					success: () => {
						uni.showToast({
							title: '复制成功',
							icon: 'success'
						});
					}
				});
			} else {
				return uni.getClipboardData();
			}
		},

		// 通用API响应处理
		handlerActionCall(res, cb) {
			if (res.code === 200 || res.code === 0) {
				cb && cb(res);
			} else {
				uni.showToast({
					icon: 'none',
					title: res.message || res.msg || '操作失败'
				});
			}
		},

		// 手机号验证
		validatePhoneNumber(phoneNumber) {
			const phoneRegex = /^1[3-9]\d{9}$/;
			return phoneRegex.test(phoneNumber);
		},
		// ==================== 用户认证相关方法 ====================

		// 用户退出登录
		async logout() {
			try {
				// 调用退出登录API
				await this.$http.post('/user/logout');

				// 清除本地存储
				uni.clearStorage({
					success: () => {
						uni.showToast({
							title: '退出成功',
							icon: 'success'
						});

						// 跳转到登录页
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}, 1000);
					}
				});
			} catch (error) {
				console.error('退出登录失败:', error);
				uni.showToast({
					title: '退出失败',
					icon: 'none'
				});
			}
		},

		// 检查登录状态
		checkLogin() {
			const { configLogin } = this;
			const token = uni.getStorageSync('userToken');
			const pages = getCurrentPages();
			const currentPage = pages[pages.length - 1];
			const currentRoute = '/' + currentPage.route;

			// 如果没有token且不在白名单中，跳转到登录页
			if (!token && !configLogin.whiteList.includes(currentRoute)) {
				uni.showToast({
					title: '请先登录',
					icon: 'none',
					duration: 1000,
					success: () => {
						setTimeout(() => {
							uni.navigateTo({
								url: configLogin.loginPage
							});
						}, 500);
					}
				});
				return false;
			}
			return true;
		}
	}
}